package com.study.elasticsearchstudy.controller;

import com.alibaba.fastjson.JSON;
import com.study.elasticsearchstudy.config.ElasticSearchConfig;
import org.apache.commons.collections4.map.HashedMap;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

@RestController
public class ElasticSearchCrudController {
    @Autowired
    RestHighLevelClient restHighLevelClient;

    /**
     * 如果不存在就新增
     * 如果存在就修改
     *
     * @return
     * @throws IOException
     */
    @RequestMapping("/index")
    public Object index() throws IOException {

        // 设置索引
        IndexRequest indexRequest = new IndexRequest("users");
        indexRequest.id("1");

        Map<String, Object> user = new HashedMap<>();
        user.put("name", "张三");
//        user.put("name", "张三三");
        user.put("age", 18);
        String jsonString = JSON.toJSONString(user);

        //设置要保存的内容，指定数据和类型
        indexRequest.source(jsonString, XContentType.JSON);

        //执行创建索引和保存数据
        IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);

        System.out.println(index);
        return index;

    }

    @RequestMapping("/update")
    public Object update() throws IOException {
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.index("users");
        updateRequest.id("1");
        Map<String, Object> user = new HashedMap<>();
        user.put("name", "张三三");
        user.put("age", 18);
        updateRequest.doc(user);
        UpdateResponse response = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
        return response;
    }

    @RequestMapping("/get")
    public Object get() throws IOException {
        GetRequest getRequest = new GetRequest("users");
        getRequest.id("1");
        GetResponse response = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
        return response;
    }

    @RequestMapping("/delete")
    public Object delete() throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest("users");
        deleteRequest.id("1");
        DeleteResponse response = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
        return response;
    }

    @RequestMapping("/search")
    public Object search() throws IOException {
        SearchRequest searchRequest = new SearchRequest("users");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("id", 1);
//        searchSourceBuilder.query(termQueryBuilder);
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(response);
        for (SearchHit hit : response.getHits().getHits()) {
            String id = hit.getId();
            System.out.println("id = " + id);
            float score = hit.getScore();
            System.out.println("score = " + score);
            Map<String, Object> source = hit.getSourceAsMap();
            System.out.println(source);
        }
        return response;
    }
}
